<?php

/**
 * platnosci actions.
 *
 * @package    ogrod3d
 * @subpackage platnosci
 * @author     Pawel Kubasiak
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class payUActions extends sfActions
{
  public function executeOnline(sfWebRequest $request)
  {
      $return = 'ERROR';
      
      if ($request->hasParameter('pos_id') && $request->hasParameter('session_id') 
          && $request->hasParameter('ts') && $request->hasParameter('sig')) {
          //Parametry przekazane w komplecie
          $platnosciArray = sfConfig::get('app_payment_method_platnosci');
          $pos_id = $platnosciArray['pos_id'];
          $key_1 = $platnosciArray['key_1'];
          $key_2 = $platnosciArray['key_2'];
          
          if ($request->getParameter('pos_id') == $pos_id) {
                $sig = md5($request->getParameter('pos_id').$request->getParameter('session_id').$request->getParameter('ts').$key_2);
                if ($request->getParameter('sig')==$sig){
                    //WSZYSTKO OK POBIERZ STATUS TRANSAKCJI
                    $ts = time();
                    $wb = new sfWebBrowser(array(),'sfCurlAdapter',array('ssl_verify'=>false));
                    $wb->post('https://www.platnosci.pl/paygw/UTF/Payment/get',array(
                            'pos_id' => $pos_id,
                            'session_id' => $request->getParameter('session_id'),
                            'ts' => $ts,
                            'sig' => md5($pos_id.$request->getParameter('session_id').$ts.$key_1)
                    ));
                    preg_match("@<trans>.*<pos_id>([0-9]*)</pos_id>.*<session_id>(.*)</session_id>.*<order_id>(.*)</order_id>.*<amount>([0-9]*)</amount>.*<status>([0-9]*)</status>.*<desc>(.*)</desc>.*<ts>([0-9]*)</ts>.*<sig>([a-z0-9]*)</sig>.*</trans>@ixs", $wb->getResponseText(), $parts);

                    if ($parts[5]) {
                        $order = OrdersPeer::retrieveBySessionId($parts[2]);
                        if ($parts[5] == '99' ) {
                            $order->confirmPayment();  
                        } else if ( $result['code'] == '2' ) {
                            $order->concelPayment();
                        }                       
                    }
                }
           }
      }
      return $this->renderText($return);
  }
}
